<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>Rem自适应js-优化flexible.js | Jason&#39;s Blog</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="Rem自适应js-优化flexible.js">
<meta property="og:type" content="article">
<meta property="og:title" content="Rem自适应js-优化flexible.js">
<meta property="og:url" content="https://xianguoji.gitee.io/myblog/2017/03/12/2017-03-12-Rem%E8%87%AA%E9%80%82%E5%BA%94js-%E4%BC%98%E5%8C%96flexible.js/index.html">
<meta property="og:site_name" content="Jason&#39;s Blog">
<meta property="og:description" content="Rem自适应js-优化flexible.js">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2017-03-11T16:00:00.000Z">
<meta property="article:modified_time" content="2020-08-12T06:46:09.012Z">
<meta property="article:author" content="Jason">
<meta property="article:tag" content="移动端">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/myblog/" title="Jason&#39;s Blog" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/myblog/css/style.css">

<meta name="generator" content="Hexo 5.0.2"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/myblog/" id="logo">人与人的悲欢并不相通，孤独是生命的本质。</a>
      </h1>
      
        <h2 id="subtitle-wrap">
          <a href="/myblog/" id="subtitle">欢迎来到我的个人网站</a>
        </h2>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/myblog/">Home</a>
        
          <a class="main-nav-link" href="/myblog/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/myblog/" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://xianguoji.gitee.io/myblog"></form>
      </div>
    </div>
  </div>
</header>
<style>
  #logo{
    text-shadow:0 0 5px #000;
    /* font-weight: bold; */
  }
  #subtitle-wrap{
    margin-top: 50px;
  }
</style>
      <div class="outer">
        <section id="main"><article id="post-2017-03-12-Rem自适应js-优化flexible.js" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/myblog/2017/03/12/2017-03-12-Rem%E8%87%AA%E9%80%82%E5%BA%94js-%E4%BC%98%E5%8C%96flexible.js/" class="article-date">
  <time datetime="2017-03-11T16:00:00.000Z" itemprop="datePublished">2017-03-12</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      Rem自适应js-优化flexible.js
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>基于阿里的一个库：lib-flexible.js，里面有一些东西方法和自定义不需要用到的，所以做了一个精简版的，另外还修复了UC浏览器竖屏与横屏转换的BUG。</p>
<h3 id="js代码"><a href="#js代码" class="headerlink" title="js代码"></a>js代码</h3><pre><code>//designWidth:设计稿的实际宽度值，需要根据实际设置
//maxWidth:制作稿的最大宽度值，需要根据实际设置
//这段js的最后面有两个参数记得要设置，一个为设计稿实际宽度，一个为制作稿最大宽度，例如设计稿为750，最大宽度为750，则为(750,750)
;(function(designWidth, maxWidth) &#123;
    var doc = document,
    win = window,
    docEl = doc.documentElement,
    remStyle = document.createElement(&quot;style&quot;),
    tid;

    function refreshRem() &#123;
        var width = docEl.getBoundingClientRect().width;
        maxWidth = maxWidth || 540;
        width&gt;maxWidth &amp;&amp; (width=maxWidth);
        var rem = width * 100 / designWidth;
        remStyle.innerHTML = &#39;html&#123;font-size:&#39; + rem + &#39;px;&#125;&#39;;
    &#125;

    if (docEl.firstElementChild) &#123;
        docEl.firstElementChild.appendChild(remStyle);
    &#125; else &#123;
        var wrap = doc.createElement(&quot;div&quot;);
        wrap.appendChild(remStyle);
        doc.write(wrap.innerHTML);
        wrap = null;
    &#125;
    //要等 wiewport 设置好后才能执行 refreshRem，不然 refreshRem 会执行2次；
    refreshRem();

    win.addEventListener(&quot;resize&quot;, function() &#123;
        clearTimeout(tid); //防止执行两次
        tid = setTimeout(refreshRem, 300);
    &#125;, false);

    win.addEventListener(&quot;pageshow&quot;, function(e) &#123;
        if (e.persisted) &#123; // 浏览器后退的时候重新计算
            clearTimeout(tid);
            tid = setTimeout(refreshRem, 300);
        &#125;
    &#125;, false);

    if (doc.readyState === &quot;complete&quot;) &#123;
        doc.body.style.fontSize = &quot;16px&quot;;
    &#125; else &#123;
        doc.addEventListener(&quot;DOMContentLoaded&quot;, function(e) &#123;
            doc.body.style.fontSize = &quot;16px&quot;;
        &#125;, false);
    &#125;
&#125;)(750, 750);</code></pre>
<h3 id="使用方法："><a href="#使用方法：" class="headerlink" title="使用方法："></a>使用方法：</h3><h4 id="1-复制上面这段代码到你的页面的头部的script标签的最前面。"><a href="#1-复制上面这段代码到你的页面的头部的script标签的最前面。" class="headerlink" title="1.复制上面这段代码到你的页面的头部的script标签的最前面。"></a>1.复制上面这段代码到你的页面的头部的script标签的最前面。</h4><h4 id="2-根据设计稿大小，调整里面的最后两个参数值。"><a href="#2-根据设计稿大小，调整里面的最后两个参数值。" class="headerlink" title="2.根据设计稿大小，调整里面的最后两个参数值。"></a>2.根据设计稿大小，调整里面的最后两个参数值。</h4><h4 id="3-使用1rem-100px转换你的设计稿的像素，例如设计稿上某个块是100px300px-换算成rem则为1rem3rem。"><a href="#3-使用1rem-100px转换你的设计稿的像素，例如设计稿上某个块是100px300px-换算成rem则为1rem3rem。" class="headerlink" title="3.使用1rem=100px转换你的设计稿的像素，例如设计稿上某个块是100px300px,换算成rem则为1rem3rem。"></a>3.使用1rem=100px转换你的设计稿的像素，例如设计稿上某个块是100px<em>300px,换算成rem则为1rem</em>3rem。</h4><p>该代码版本区别于手淘的rem换算方法。使用的是1rem=100px的换算。</p>
<p>假如你有一个块是.box{width:120px;height:80px;} 转为rem则为.box{width:1.2rem; height:.8rem;}</p>
<h3 id="基本的HTML模板"><a href="#基本的HTML模板" class="headerlink" title="基本的HTML模板"></a>基本的HTML模板</h3><p>你也可以直接复制下面这个基础的HTML模板。</p>
<pre><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset=&quot;utf-8&quot;&gt;
&lt;meta content=&quot;width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no&quot; name=&quot;viewport&quot;&gt;
&lt;meta content=&quot;yes&quot; name=&quot;apple-mobile-web-app-capable&quot;&gt;
&lt;meta content=&quot;black&quot; name=&quot;apple-mobile-web-app-status-bar-style&quot;&gt;
&lt;meta content=&quot;telephone=no&quot; name=&quot;format-detection&quot;&gt;
&lt;meta content=&quot;email=no&quot; name=&quot;format-detection&quot;&gt;
&lt;meta name=&quot;description&quot; content=&quot;不超过150个字符&quot;/&gt;
&lt;meta name=&quot;keywords&quot; content=&quot;&quot;/&gt;
&lt;meta content=&quot;caibaojian&quot; name=&quot;author&quot;/&gt;
&lt;title&gt;前端开发博客&lt;/title&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;base.css&quot;&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
//引入该flexible.min.js
!function(e,t)&#123;function n()&#123;var n=l.getBoundingClientRect().width;t=t||540,n&gt;t&amp;&amp;(n=t);var i=100*n/e;r.innerHTML=&quot;html&#123;font-size:&quot;+i+&quot;px;&#125;&quot;&#125;var i,d=document,o=window,l=d.documentElement,r=document.createElement(&quot;style&quot;);if(l.firstElementChild)l.firstElementChild.appendChild(r);else&#123;var a=d.createElement(&quot;div&quot;);a.appendChild(r),d.write(a.innerHTML),a=null&#125;n(),o.addEventListener(&quot;resize&quot;,function()&#123;clearTimeout(i),i=setTimeout(n,300)&#125;,!1),o.addEventListener(&quot;pageshow&quot;,function(e)&#123;e.persisted&amp;&amp;(clearTimeout(i),i=setTimeout(n,300))&#125;,!1),&quot;complete&quot;===d.readyState?d.body.style.fontSize=&quot;16px&quot;:d.addEventListener(&quot;DOMContentLoaded&quot;,function(e)&#123;d.body.style.fontSize=&quot;16px&quot;&#125;,!1)&#125;(750,750);
&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
    &lt;!-- 正文 --&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>
<h3 id="base-css"><a href="#base-css" class="headerlink" title="base.css"></a>base.css</h3><pre><code>body,dl,dd,ul,ol,h1,h2,h3,h4,h5,h6,pre,form,input,textarea,p,hr,thead,tbody,tfoot,th,td&#123;margin:0;padding:0;&#125;
ul,ol&#123;list-style:none;&#125;
a&#123;text-decoration:none;&#125;
html&#123;-ms-text-size-adjust:none;-webkit-text-size-adjust:none;text-size-adjust:none;&#125;
body&#123;line-height:1.5; font-size:14px;&#125;
body,button,input,select,textarea&#123;font-family:&#39;helvetica neue&#39;,tahoma,&#39;hiragino sans gb&#39;,stheiti,&#39;wenquanyi micro hei&#39;,\5FAE\8F6F\96C5\9ED1,\5B8B\4F53,sans-serif;&#125;
b,strong&#123;font-weight:bold;&#125;
i,em&#123;font-style:normal;&#125;
table&#123;border-collapse:collapse;border-spacing:0;&#125;
table th,table td&#123;border:1px solid #ddd;padding:5px;&#125;
table th&#123;font-weight:inherit;border-bottom-width:2px;border-bottom-color:#ccc;&#125;
img&#123;border:0 none;width:auto\9;max-width:100%;vertical-align:top; height:auto;&#125;
button,input,select,textarea&#123;font-family:inherit;font-size:100%;margin:0;vertical-align:baseline;&#125;
button,html input[type=&quot;button&quot;],input[type=&quot;reset&quot;],input[type=&quot;submit&quot;]&#123;-webkit-appearance:button;cursor:pointer;&#125;
button[disabled],input[disabled]&#123;cursor:default;&#125;
input[type=&quot;checkbox&quot;],input[type=&quot;radio&quot;]&#123;box-sizing:border-box;padding:0;&#125;
input[type=&quot;search&quot;]&#123;-webkit-appearance:textfield;-moz-box-sizing:content-box;-webkit-box-sizing:content-box;box-sizing:content-box;&#125;
input[type=&quot;search&quot;]::-webkit-search-decoration&#123;-webkit-appearance:none;&#125;
input:focus&#123;outline:none;&#125;
select[size],select[multiple],select[size][multiple]&#123;border:1px solid #AAA;padding:0;&#125;
article,aside,details,figcaption,figure,footer,header,hgroup,main,nav,section,summary&#123;display:block;&#125;
audio,canvas,video,progress&#123;display:inline-block;&#125;
body&#123;background:#fff;&#125;
input::-webkit-input-speech-button &#123;display: none&#125;
button,input,textarea&#123;
-webkit-tap-highlight-color: rgba(0,0,0,0);
&#125;</code></pre>
<h3 id="再次强调一下："><a href="#再次强调一下：" class="headerlink" title="再次强调一下："></a>再次强调一下：</h3><p>需要根据你的设计稿的大小来调整脚本最后的两个参数。</p>
<pre><code>&#125;)(750, 750);</code></pre>
<p>第一个参数是设计稿的宽度，一般设计稿有640，或者是750，你可以根据实际调整</p>
<p>第二个参数则是设置制作稿的最大宽度，超过750，则以750为最大限制。</p>
<p>文章转自-链接:<a target="_blank" rel="noopener" href="http://caibaojian.com/simple-flexible.html">http://caibaojian.com/simple-flexible.html</a></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://xianguoji.gitee.io/myblog/2017/03/12/2017-03-12-Rem%E8%87%AA%E9%80%82%E5%BA%94js-%E4%BC%98%E5%8C%96flexible.js/" data-id="ckds6fxa5000dk8qch6yaeg9s" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/myblog/tags/%E7%A7%BB%E5%8A%A8%E7%AB%AF/" rel="tag">移动端</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/myblog/2017/04/20/2017-04-20-%E9%94%A4%E5%AD%90%E7%A7%91%E6%8A%80%E5%AE%98%E7%BD%91banner3D%E6%95%88%E6%9E%9C/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          锤子科技官网banner3D效果
        
      </div>
    </a>
  
  
    <a href="/myblog/2017/03/12/2017-03-12-jsonp%E8%B7%A8%E5%9F%9F/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">jsonp跨域请求</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/myblog/tags/javascript/" rel="tag">javascript</a></li><li class="tag-list-item"><a class="tag-list-link" href="/myblog/tags/js/" rel="tag">js</a></li><li class="tag-list-item"><a class="tag-list-link" href="/myblog/tags/rem/" rel="tag">rem</a></li><li class="tag-list-item"><a class="tag-list-link" href="/myblog/tags/%E5%85%B6%E4%BB%96/" rel="tag">其他</a></li><li class="tag-list-item"><a class="tag-list-link" href="/myblog/tags/%E5%8D%9A%E5%AE%A2/" rel="tag">博客</a></li><li class="tag-list-item"><a class="tag-list-link" href="/myblog/tags/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/" rel="tag">性能优化</a></li><li class="tag-list-item"><a class="tag-list-link" href="/myblog/tags/%E6%A1%86%E6%9E%B6/" rel="tag">框架</a></li><li class="tag-list-item"><a class="tag-list-link" href="/myblog/tags/%E7%A7%BB%E5%8A%A8%E7%AB%AF/" rel="tag">移动端</a></li><li class="tag-list-item"><a class="tag-list-link" href="/myblog/tags/%E8%B7%A8%E5%9F%9F%E8%AF%B7%E6%B1%82/" rel="tag">跨域请求</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/myblog/tags/javascript/" style="font-size: 20px;">javascript</a> <a href="/myblog/tags/js/" style="font-size: 10px;">js</a> <a href="/myblog/tags/rem/" style="font-size: 16.67px;">rem</a> <a href="/myblog/tags/%E5%85%B6%E4%BB%96/" style="font-size: 10px;">其他</a> <a href="/myblog/tags/%E5%8D%9A%E5%AE%A2/" style="font-size: 13.33px;">博客</a> <a href="/myblog/tags/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/" style="font-size: 13.33px;">性能优化</a> <a href="/myblog/tags/%E6%A1%86%E6%9E%B6/" style="font-size: 10px;">框架</a> <a href="/myblog/tags/%E7%A7%BB%E5%8A%A8%E7%AB%AF/" style="font-size: 13.33px;">移动端</a> <a href="/myblog/tags/%E8%B7%A8%E5%9F%9F%E8%AF%B7%E6%B1%82/" style="font-size: 10px;">跨域请求</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/myblog/archives/2020/08/">August 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/myblog/archives/2017/06/">June 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/myblog/archives/2017/05/">May 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/myblog/archives/2017/04/">April 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/myblog/archives/2017/03/">March 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/myblog/archives/2017/02/">February 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/myblog/archives/2017/01/">January 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/myblog/archives/2016/10/">October 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/myblog/archives/2016/09/">September 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/myblog/archives/2016/08/">August 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/myblog/archives/2015/08/">August 2015</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/myblog/2020/08/13/2020-08-13%E4%B8%AA%E4%BA%BA%E7%AE%80%E5%8E%86/">(no title)</a>
          </li>
        
          <li>
            <a href="/myblog/2017/06/18/2017-06-18-%E5%89%8D%E7%AB%AF%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96%E4%B8%80/">前端性能优化一</a>
          </li>
        
          <li>
            <a href="/myblog/2017/05/15/2017-05-15-%E8%B0%83%E7%94%A8%E7%99%BE%E5%BA%A6%E5%9C%B0%E5%9B%BE%E6%8F%92%E4%BB%B6-%E6%9B%B4%E6%96%B0/">调用百度地图插件-更新</a>
          </li>
        
          <li>
            <a href="/myblog/2017/04/23/2017-04-23-vue%E6%90%AD%E5%BB%BA%E9%A1%B9%E7%9B%AE/">vue搭建项目</a>
          </li>
        
          <li>
            <a href="/myblog/2017/04/21/2017-04-21-%E8%B0%83%E7%94%A8%E7%99%BE%E5%BA%A6%E5%9C%B0%E5%9B%BE%E6%8F%92%E4%BB%B6/">调用百度地图插件</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 Jason<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/myblog/" class="mobile-nav-link">Home</a>
  
    <a href="/myblog/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/myblog/fancybox/jquery.fancybox.css">

  
<script src="/myblog/fancybox/jquery.fancybox.pack.js"></script>




<script src="/myblog/js/script.js"></script>




  </div>
</body>
</html>